package com.wonderful.compose.progress

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import com.wonderful.compose.R
import com.wonderful.compose.ui.theme.BookingBackground
import com.wonderful.compose.ui.theme.ComposeLearnTheme

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/2/19 14:39
 */
class FlyProgressActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeLearnTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = BookingBackground
                ) {
                    FlyProgressView()
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun FlyProgressView() {
    var healthPercentage by remember {
        mutableFloatStateOf(1.0f)
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        //SquareHealth(healthPercentage = healthPercentage)
        FlyHealth(healthPercentage = healthPercentage)

        Spacer(modifier = Modifier.height(20.dp))

        Button(
            colors = ButtonDefaults.buttonColors(containerColor = Color.Red),
            onClick = {
                if (healthPercentage > 0) {
                    healthPercentage -= 0.1f
                } else {
                    healthPercentage = 1f
                }
            }) {
            Text(text = "HIT")
        }
    }
}

@Composable
fun SquareHealth(healthPercentage: Float) {
    Box(
        modifier = Modifier
            .size(240.dp)
            .background(Color.LightGray)
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height((240 * healthPercentage).dp)
                .background(Color.Green)
                .align(Alignment.BottomCenter)
        )
    }
}

@Composable
fun FlyHealth(healthPercentage: Float) {
    val flySize = 240
    val flyBitmap = ImageBitmap.imageResource(id = R.drawable.fly)

    Box(modifier = Modifier
        .size(flySize.dp)
        .drawWithContent {
            drawContent()
            drawImage(
                image = flyBitmap,
                dstSize = IntSize(
                    width = flySize.dp
                        .toPx()
                        .toInt(),
                    height = flySize.dp
                        .toPx()
                        .toInt()
                ),
                blendMode = BlendMode.DstIn
            )
        }
    ) {
        Box(
            modifier = Modifier
                .size(flySize.dp)
                .background(Color.LightGray)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height((flySize * healthPercentage).dp)
                    .background(Color.Green)
                    .align(Alignment.BottomCenter)
            )
        }
    }
}